import utils from '../../../utils/utils'
import { FreeOrderHttp } from '../../../api/freeOrder.js'

const freeOrderModel = new FreeOrderHttp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: '', 
    // 详情类型
    // >>> orderMoney - 订单金额详情
    // >>> freeOrderMoney - 免单金额详情
    // >>> freeOrderRedpacket - 获取免单红包详情
    // >>> myInvite - 我的邀请
    orderId: '', // 订单id
    info: {}, // 相关信息
    list: [], // 列表
    page: 1,
    pageSize: 10,
    totalNum: 0,
  },

  initPage (options) {
    if (options.type) {
      let setDataObj = {}
      setDataObj.type = options.type // 详情类型
      if (options.orderId) {
        setDataObj.orderId = options.orderId // 订单id
      }
      this.setData(setDataObj)
      this.typeRequest()
    }
  },

  typeRequest () {
    const { type } = this.data
    switch (type) {
      case 'orderMoney':
        wx.setNavigationBarTitle({title:'累计订单金额详情'});
        this.httpGetMyFreeOrderList()
        break
      case 'freeOrderMoney':
        wx.setNavigationBarTitle({title:'累计免单金额详情'});
        this.httpGetFreeOrderMoneyList()
        break
      case 'freeOrderRedpacket':
        wx.setNavigationBarTitle({title:'累计获得免单红包'});
        this.httpGetFreeOrderRedpacketList()
        break
      case 'myInvite':
        wx.setNavigationBarTitle({title:'我邀请的'});
        this.httpGetMyInviteList()
        break
      default:
        break
    }
  },

  // 继续免单
  onContinueFreeOrder (event) {
    const { orderid } = event.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/freeOrder/invite/invite?orderId=${orderid}`
    })
  },

  // 页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh: async function () {
    this.setData({
      page: 1
    })
    await ths.typeRequest()
    wx.stopPullDownRefresh()
  },

  // 页面上拉触底事件的处理函数
  onReachBottom: function () {
    const { list, totalNum, page } = this.data
    if (list.length < totalNum) {
      this.setData({
        page: page + 1
      })
      ths.typeRequest()
    }
  },

  // 获取我的可免单订单列表
  httpGetMyFreeOrderList () {
    const { list, page, pageSize } = this.data
    return freeOrderModel.getMyFreeOrderList({
      page: page,
      page_size: pageSize
    }).then((res) => {
      let orderList = page === 1 ? res.free_order_list : list.concat(res.free_order_list)
      this.setData({
        list: orderList,
        info: {
          today_free_order_num: res.today_free_order_num,
          today_free_order_price: res.today_free_order_price
        },
        totalNum: res.total
      })
    })
  },

  // 获取累计免单金额列表
  httpGetFreeOrderMoneyList () {
    const { list, page, pageSize } = this.data
    return freeOrderModel.getFreeOrderMoneyList({
      page: page,
      page_size: pageSize
    }).then((res) => {
      let orderMoneyList = page === 1 ? res.free_order_record_list : list.concat(res.free_order_record_list)
      this.setData({
        list: orderMoneyList,
        info: {},
        totalNum: res.total
      })
    })
  },

  // 获取累计免单红包列表
  httpGetFreeOrderRedpacketList () {
    const { list, page, pageSize } = this.data
    return freeOrderModel.getFreeOrderRedpacketList({
      page: page,
      page_size: pageSize
    }).then((res) =>{
      let redpacektList = page === 1 ? res.cumulative_redpackage_list : list.concat(res.cumulative_redpackage_list)
      this.setData({
        list: redpacektList,
        info: {
          cumulative_price: res.cumulative_price,
          my_help_free_order_num: res.my_help_free_order_num
        },
        totalNum: res.total
      })
    })
  },

  // 获取我邀请的类表
  httpGetMyInviteList () {
    const { orderId, list, page, pageSize } = this.data
    return freeOrderModel.getMyInviteList({
      orderId: orderId,
      page: page,
      page_size: pageSize
    }).then((res) => {
      let inviteList = page === 1 ? res.free_order_record_list : list.concat(res.free_order_record_list)
      this.setData({
        list: inviteList,
        info: {},
        totalNum: res.total
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})